class MyStack {
private:
    queue<int> Q1;
    queue<int> Q2;

public:
    MyStack() {}
    
    void push(int x) {
        queue<int> &srcQ = Q1.empty() ? Q2 : Q1;
        srcQ.push(x);
    }
    
    int pop() {
        queue<int> &srcQ = Q1.empty() ? Q2 : Q1;
        queue<int> &dstQ = Q1.empty() ? Q1 : Q2;

        while(srcQ.size() > 1){
            dstQ.push(srcQ.front());
            srcQ.pop();
        }

        int rst = srcQ.front();
        srcQ.pop();
        return rst;
    }
    
    int top() {
        queue<int> &srcQ = Q1.empty() ? Q2 : Q1;
        queue<int> &dstQ = Q1.empty() ? Q1 : Q2;

        while(srcQ.size() > 1){
            dstQ.push(srcQ.front());
            srcQ.pop();
        }

        int rst = srcQ.front();
        dstQ.push(rst);
        srcQ.pop();
        return rst;
    }
    
    bool empty() {
        return (Q1.empty() && Q2.empty());
    }
};

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack* obj = new MyStack();
 * obj->push(x);
 * int param_2 = obj->pop();
 * int param_3 = obj->top();
 * bool param_4 = obj->empty();
 */